package org.zfes.snowier.cloud.zbss.metting.biz.service.impl;

import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZStrUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.metting.biz.service.IMettingsNotifyService;
import org.zfes.snowier.cloud.zbss.metting.biz.dao.MettingsNotifyMapper;
import org.zfes.snowier.cloud.zbss.metting.biz.dto.MettingsNotifyCreateDto;
import org.zfes.snowier.cloud.zbss.metting.biz.model.MettingsNotify;
import java.util.stream.Stream;
@Service
public class MettingsNotifyServiceImpl implements IMettingsNotifyService{
 	@Autowired
	private MettingsNotifyMapper mettingsNotifyMapper;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createMettingsNotify(Long creatorId,String creatorName,MettingsNotifyCreateDto notifyCreateDto) {
		
		ZBeanUtil.validateBean(notifyCreateDto);
		
		MettingsNotify mettingsNotify=new MettingsNotify();
		ZBeanUtil.copy(notifyCreateDto, mettingsNotify, true);
		if(creatorId!=null) {
			mettingsNotify.setCreatorId(creatorId);
		}
		if(ZStrUtil.hasText(creatorName)) {
			mettingsNotify.setCreatorName(creatorName);
		}
		
		ZBeanUtil.setCreateInfo(mettingsNotify);
		mettingsNotifyMapper.insertSelective(mettingsNotify);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<MettingsNotify> loadMettingsNotifyById(Long id) {
		if(id!=null){
			return Optional.ofNullable(mettingsNotifyMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateMettingsNotify(MettingsNotify mettingsNotify) {
		ZBeanUtil.setUpdatedInfo(mettingsNotify);
		ZBeanUtil.validateBean(mettingsNotify);
		mettingsNotifyMapper.updateByPrimaryKeySelective(mettingsNotify);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteMettingsNotify(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		setMettingsNotifyStatus(id, Byte.valueOf("-1"));
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setMettingsNotifyStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		mettingsNotifyMapper.updateStatus( id,  status) ;
	}
	
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void addReadCount(Long id, Integer addCountTobeAdd) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(addCountTobeAdd, "状态参数错误");
		mettingsNotifyMapper.uppdateAddReadCount( id,  addCountTobeAdd) ;
	}
	
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadMettingsNotifyDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(mettingsNotifyMapper.selectListCount(pm), mettingsNotifyMapper.selectMapListPage(pm));
		return ds;
	}
	
}